﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text;
using System.Xml.Linq;
using Microsoft.Phone.Tasks;


namespace lookbookdotnu
{
    public partial class PivotPage1 : PhoneApplicationPage
    {
        public PivotPage1()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //// TODO: Add event handler implementation here.
            //if (!App.ViewModel.IsDataLoaded)
            //{
            //    App.ViewModel.LoadData();
            //}
            WebClient mainfeed1 = new WebClient();
            mainfeed1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(mainfeed1_DownloadStringCompleted);
            mainfeed1.DownloadStringAsync(new Uri("http://lookbook.nu/rss", UriKind.RelativeOrAbsolute));

            WebClient girlsfeed1 = new WebClient();
            girlsfeed1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(girlsfeed1_DownloadStringCompleted);
            girlsfeed1.DownloadStringAsync(new Uri("http://lookbook.nu/rss?gender=girls",UriKind.RelativeOrAbsolute));

            WebClient guysf = new WebClient();
            guysf.DownloadStringCompleted += new DownloadStringCompletedEventHandler(guysf_DownloadStringCompleted);
            guysf.DownloadStringAsync(new Uri("http://lookbook.nu/rss?gender=guys", UriKind.RelativeOrAbsolute));

            WebClient twt = new WebClient();
            twt.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twt_DownloadStringCompleted);
            twt.DownloadStringAsync(new Uri("http://twitter.com/statuses/user_timeline/lookbookdotnu.rss", UriKind.RelativeOrAbsolute));
        }

        void twt_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }
            //throw new NotImplementedException();
            XElement twtEl = XElement.Parse(e.Result);
            var items_t = from item in twtEl.Descendants("channel")
                          from tt in item.Descendants("item")
                          select new twitterrss
                          {
                              titletwt = tt.Element("title").Value,
                              pdate = tt.Element("pubDate").Value,
                              desrtwt = tt.Element("description").Value,
                              twlink = tt.Element("link").Value
                          };

            twtlst.ItemsSource = items_t;
        }

        void guysf_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //throw new NotImplementedException();

            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }

            XElement guysEl = XElement.Parse(e.Result);
            var items_b = from item in guysEl.Descendants("channel")
                          from gg in item.Descendants("item")
                          select new guysfeed1
                          {
                              title_b = gg.Element("title").Value,
                              linkte_guy = gg.Element("link").Value,
                              date_b = gg.Element("pubDate").Value,
                              //description = aa.Element("description").Value,
                              ImgSrc_b = gg.Element("imageSrc").Value//.Attribute("url").Value
                          };
            Guyslst.ItemsSource = items_b;
            //Girlslst.ItemsSource = items_g;
        }

        void girlsfeed1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }

            //txtXML.Text = e.Result;

            XElement girlsfeedEl = XElement.Parse(e.Result);
            var items_g = from item in girlsfeedEl.Descendants("channel")
                        from bb in item.Descendants("item")
                        select new girlsfeed
                        {
                            title_g = bb.Element("title").Value,
                            linkte = bb.Element("link").Value,
                            date_g = bb.Element("pubDate").Value,
                            //description = aa.Element("description").Value,
                            ImgSrc_g = bb.Element("imageSrc").Value//.Attribute("url").Value
                        };
            Girlslst.ItemsSource = items_g;

        }

        void mainfeed1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }

            XElement mainfeedEl = XElement.Parse(e.Result);
            var items = from item in mainfeedEl.Descendants("channel")
                        from aa in item.Descendants("item")
                        select new MainFeedff
                        {
                            title = aa.Element("title").Value,
                            link_Main = aa.Element("link").Value,
                            date = aa.Element("pubDate").Value,
                            ImgSrc = aa.Element("imageSrc").Value
                        };
            Mainfeedlst.ItemsSource = items;

        }

        private void Image_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //string site;
            //site = textbox1.Text;
            //webb1.Navigate(new Uri(site, UriKind.Absolute));
            ////webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
        }

        private void Mainfeedlst_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //// TODO: Add event handler implementation here.
            MainFeedff objm = (MainFeedff)Mainfeedlst.SelectedValue;//ตอน event ทีร่จะใช้ลิ้ง
            WebBrowserTask webb = new WebBrowserTask();
            webb.URL = objm.link_Main.ToString();
            webb.Show();
        }

        private void Girlslst_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            girlsfeed objgi = (girlsfeed)Girlslst.SelectedValue;
            WebBrowserTask webgirls = new WebBrowserTask();
            webgirls.URL = objgi.linkte.ToString();
            webgirls.Show();

            
        }

        private void Guyslst_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            guysfeed1 objgu = (guysfeed1)Guyslst.SelectedValue;
            WebBrowserTask webb3 = new WebBrowserTask();
            webb3.URL = objgu.linkte_guy.ToString();
            webb3.Show();
        }

        private void twtlst_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            twitterrss objtw = (twitterrss)twtlst.SelectedValue;
            WebBrowserTask twweb = new WebBrowserTask();
            twweb.URL = objtw.twlink.ToString();
            twweb.Show();
        }
    }
}
